import greendbgui.Role
import greendbgui.User
import greendbgui.UserRole

class BootStrap 
{
    def init = 
    { servletContext ->
    
        def userRole = Role.findByAuthority('ROLE_USER') ?: new Role(authority: 'ROLE_USER').save(failOnError: true)
        def adminRole = Role.findByAuthority('ROLE_ADMIN') ?: new Role(authority: 'ROLE_ADMIN').save(failOnError: true)
                
        def adminUser = User.findByUsername('admin') ?: 
            new User (username: 'admin',
                      password: 'admin',
                      enabled: true).save(failOnError: true)
 
        if (!adminUser.authorities.contains(adminRole)) 
        {
            UserRole.create adminUser, adminRole
        }

          def brynUser = User.findByUsername('bryn') ?: 
            new User (username: 'bryn',
                      password: 'aerosol',
                      enabled: true).save(failOnError: true)
 
        if (!brynUser.authorities.contains(userRole)) 
        {
            UserRole.create brynUser, userRole
        }
    }
    
    def destroy = 
    {
    }
}
